﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class City
    {
        #region Properties
        public Country Country
        {
            get 
            {
                return this.GetCountry();
            }
        }
        #endregion

        #region  Methods
        public Country GetCountry()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Countries.Where(p => p.ID == this.ID_Country);
            if (v.Count() == 0)
                return null;
            return v.First();
        }
        #endregion

        #region Static Methods
        public static City GetCity(String name)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Cities.Where(p => p.Name.ToLower().Contains(@name.ToLower()));
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public static List<City> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Cities.OrderByDescending(p => p.ID);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<City> GetList(int countryID)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Cities.Where(p => p.ID_Country == countryID).OrderByDescending(p => p.ID);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(City city)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (city.Name == "")
                errorList.Add(new ErrorItem("Name", "City Name is required."));
            if(city.Name.Length > 200)
                errorList.Add(new ErrorItem("Name", "City Name is only allow up to 200 characters."));
            if(city.ID_Country < 1)
                errorList.Add(new ErrorItem("ID_Country", "Country is required."));
            return errorList;
        }

        public static List<ErrorItem> Insert(ref City city)
        {
            List<ErrorItem> errorList = InputInfomationValidation(city);
            if (errorList.Count == 0)
            {
                System.Object ob = (System.Object)city;
                if (!Provider.Insert(ref ob, "Cities"))
                    errorList.Add(new ErrorItem("Insert", "Failed."));
            }
            return errorList;
        }

        public static List<ErrorItem> Insert(ref City city, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            List<ErrorItem> errorList = InputInfomationValidation(city);

            City temp = GetCity(city.Name);
            if (temp != null)
            {
                city = temp;
                return errorList;
            }
            if (errorList.Count == 0)
            {
                Nullable<Int32> id_City = new int();
                int errorMess = _db.InsertCity(ref id_City, city.ID_Country, city.Name);
                if (errorMess == 1)
                    city.ID = id_City.Value;
                
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> Delete(int id_City)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            List<ErrorItem> errorList = new List<ErrorItem>();

            City di = _db.Cities.Single(p => p.ID == id_City);
            if (Provider.Delete(di, "Cities"))
                errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Where(p => p.ID_ErrorMessage == 1).First().Description));
            else
                errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Where(p => p.ID_ErrorMessage == 54).First().Description));

            return errorList;
        }
        #endregion


    }
}